<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class ShowController extends Controller{
	private $goodsModel;
	private $gattrModel;
	private $attrModel;
	private $cateModel;
	private $glistModel;
	//构造函数
	public function __init(){
		$this->goodsModel = new \Common\Model\Goods;
		$this->gattrModel = new \Common\Model\Gattr;
		$this->attrModel = new \Common\Model\Attr;
		$this->cateModel = new \Common\Model\Category;
		$this->glistModel = new \Common\Model\Glist;
	}
	
    //动作
    public function index(){
    	$gid = Q('get.gid',0,'intval');
//		得到商品基本信息+详细信息
		$data = $this->goodsModel->join('gdetail','gid','=','goods_gid')->where("gid = {$gid}")->find();
		$data['mini'] = explode(',', $data['mini']);
		$data['medium'] = explode(',', $data['medium']);
		$data['big'] = explode(',', $data['big']);
		View::with('data',$data);
		
		
//		规格属性
//		获得类型id
		$tid = $this->goodsModel->where("gid = {$gid}")->pluck("type_tid");
//		获得atid
		$tmp = $this->attrModel->where("type_tid = {$tid} and class=1")->field('atid')->get();
//		得到atid数组
		$atid = array();
		foreach ($tmp as $k => $v) {
			$atid[] = $v['atid'];
		}
//		定义空数组用来接受数据
		$spec = array();
		foreach ($atid as $k => $v) {
//			查询所需要的内容
			$data = $this->gattrModel->join('attribute','atid','=','attribute_atid')->where("goods_gid = {$gid} and atid = {$v}")->field('gaid,gavalue,atname')->get();
//			将数组进行合并
			foreach ($data as $kk => $vv) {
				$spec[$v]['gaid'][]=$vv['gaid'];
				$spec[$v]['gavalue'][]=$vv['gavalue'];
				$spec[$v]['atname']=$vv['atname'];
			}
		}
		View::with("spec",$spec);
		
		
		
		
		
//		商品路径
//		获得商品的cid
		$cateAll = $this->cateModel->get();
		$cid = $this->goodsModel->where("gid = {$gid}")->pluck("category_cid");
		$pid = $this->cateModel->where("cid = {$cid}")->pluck("pid");
		$pids = $this->cateModel->getParents($cateAll,$pid);
//		数组倒转
		$pids = array_reverse($pids);
		$pids[] = $cid;
		$pData = $this->cateModel->where("cid IN(".implode(',', $pids).")")->field('cname,cid')->get();
		View::with("pData",$pData);
		
    	View::make();
    }
	
	
	
}
